'use strict';

//拼接字符串
function splice(arr01) {
    var str01 = '欢迎';
    for (let i = 0; i < arr01.length; i++) {
        str01 += ',' + arr01[i];
    }
    str01 += '同学!';
    //var index01 = str01.lastIndexOf(',');
    //console.log(index01);
    var arr02 = str01.split('');
    var index02 = arr02.lastIndexOf(',');

    arr02.splice(index02, 1, '和');

    var str02 = '';
    for (let i = 0; i < arr02.length; i++) {
        str02 += arr02[i];
    }

    //console.log(arr02);
    console.log(str02);
}

splice(['张三', '李四', '王五', '赵六', '秦七', 'js'])

//最长前缀
var longestCommonPrefix2 = function(strs) {
    var str01 = "";
    var length = strs[0].length;
    for (let k = 1; k < strs.length; k++) {
        if (length < strs[k].length) {
            length = strs[k].length;
        }

    }

    for (let i = 0; i < length; i++) {
        for (let j = 1; j < strs.length; j++) {
            if (strs[j][i] != strs[0][i]) return str01
        }
        str01 += strs[0][i];
    }
    return str01;
};
console.log(longestCommonPrefix2(["dog", "racecar", "car"]));

//给你两个字符串 haystack 和 needle ，
// 请你在 haystack 字符串中找出 needle 字符串出现的第一个位置（下标从 0 开始）。
// 如果不存在，则返回  -1 。


var strStr = function(haystack, needle) {
    const length0 = haystack.length
    const lenght1 = needle.length
    if(lenght1===0){
        return 0
    }
    for(let i =0 ;  i < length0; i++){
        const str = haystack.slice(i, i+lenght1)
        if(str===needle){
            return i
        }
    }
    return  -1
};
console.log(strStr("hello","ll"))

// "mississippi"
// "issip"